#!/bin/bash

yum install -y autoconf cmake gcc gcc-c++ supervisor git

# bash: ./install.sh: /bin/bash^M: bad interpreter
# sed -i "s/\r//" install.sh

unzip_tgz() {
    tar -zxf /data/server/php/extension/gd.tgz -C /tmp/extension/
    tar -zxf /data/server/php/extension/opcache.tgz -C /tmp/extension/
    tar -zxf /data/server/php/extension/redis-5.3.5.tgz -C /tmp/extension/
    tar -zxf /data/server/php/extension/swoole-v4.8.5.tgz -C /tmp/extension/
    tar -zxf /data/server/php/extension/xdebug-3.1.3.tgz -C /tmp/extension/
}

install_redis() {
    cd /tmp/extension/redis-5.3.5
    /usr/local/php/bin/phpize
    ./configure --with-php-config=/usr/local/php/bin/php-config 
    make && make install >> /dev/null

    EXTENSION_DIR=$(php-config --extension-dir)
    if [ -f "${EXTENSION_DIR}/redis.so" ]; then
        echo 'extension=redis.so' > /data/server/php/ini/ext-redis.ini
    fi
}

install_xdebug() {
    cd /tmp/extension/xdebug-3.1.3
    /usr/local/php/bin/phpize
    ./configure --with-php-config=/usr/local/php/bin/php-config 
    make && make install >> /dev/null
}

install_gd() {
    cd /tmp/extension/gd
    /usr/local/php/bin/phpize
    ./configure --with-php-config=/usr/local/php/bin/php-config 
    make && make install >> /dev/null

    EXTENSION_DIR=$(php-config --extension-dir)
    if [ -f "${EXTENSION_DIR}/gd.so" ]; then
        echo 'extension=gd.so' > /data/server/php/ini/ext-gd.ini
    fi
}

install_opcache() {
    cd /tmp/extension/opcache
    /usr/local/php/bin/phpize
    ./configure --with-php-config=/usr/local/php/bin/php-config 
    make && make install >> /dev/null
}

install_swoole() {
    cd /tmp/extension/swoole-v4.8.5
    /usr/local/php/bin/phpize
    ./configure --with-php-config=/usr/local/php/bin/php-config 
    make && make install >> /dev/null

    EXTENSION_DIR=$(php-config --extension-dir)
    if [ -f "${EXTENSION_DIR}/swoole.so" ]; then
        echo 'extension=swoole.so' > /data/server/php/ini/ext-swoole.ini
    fi
}

[ ! -d "/tmp/extension" ] &&  mkdir /tmp/extension

# 解压文件
unzip_tgz

# 依次安装
install_gd
install_opcache
install_redis
install_xdebug
install_swoole

# 删除目录
rm -fr /tmp/extension/*